package cn.samler.diandi.api.controller;

import cn.samler.diandi.api.entity.PostRequestComment;
import cn.samler.diandi.api.model.io.Response;
import cn.samler.diandi.api.model.io.request.post.Evaluate;
import cn.samler.diandi.api.model.io.request.post.RequestMark;
import cn.samler.diandi.api.model.io.request.post.request.PostRequestCommentList;
import cn.samler.diandi.api.model.io.request.post.request.PostRequestList;
import cn.samler.diandi.api.service.RequestService;
import cn.samler.diandi.api.utils.ResponseBuilder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("request")
public class RequestController {
    private final RequestService requestService;

    public RequestController(RequestService requestService) {
        this.requestService = requestService;
    }

    /**
     * 查询单个订单
     *
     * @param id request id
     * @return Response
     */
    @GetMapping("detail/{id}")
    public Response detail(@PathVariable("id") String id) {
        return requestService.detail(id);
    }

    /**
     * 列出需求订单列表
     *
     * @param form 请求表单
     * @return Response
     */
    @PostMapping("list")
    public Response list(@RequestBody @Valid PostRequestList form) {
        return requestService.list(form);
    }

    /**
     * 提交申请
     *
     * @param id post id
     * @return Response
     */
    @GetMapping("commit/{id}")
    public Response commitRequest(@PathVariable("id") Integer id) {
        return requestService.commit(id);
    }

    /**
     * 评价一个已结束的定单
     *
     * @param form 评价表单
     * @return Response
     */
    @PostMapping("evaluate")
    public Response evaluate(@RequestBody @Valid Evaluate form) {
        Response resp;
        try {
            resp = requestService.evaluate(form);
        } catch (RuntimeException e) {
            resp = new ResponseBuilder().failure().msg(e.getMessage()).build();
        }

        return resp;
    }

    /**
     * 结束一个定单
     *
     * @param form Request标识实体
     * @return Response
     */
    @PostMapping("over")
    public Response over(@RequestBody @Valid RequestMark form) {
        return requestService.over(form);
    }

    /**
     * 发表一个匹配定单的评论
     *
     * @param form 评论表单
     * @return Response
     */
    @PostMapping("comment")
    public Response comment(@RequestBody @Valid PostRequestComment form) {
        return requestService.comment(form);
    }

    /**
     * 获得评论列表
     *
     * @param form 分页表单
     * @return Response
     */
    @PostMapping("comment/list")
    public Response listComment(@RequestBody @Valid PostRequestCommentList form) {
        return requestService.listComment(form);
    }

    /**
     * 撤销request
     *
     * @param form Request标识实体
     * @return Response
     */
    @PostMapping("cancel")
    public Response cancel(@RequestBody @Valid RequestMark form) {
        return requestService.cancel(form);
    }

    /**
     * 同意一个申请订单
     *
     * @param form Request标识实体
     * @return Response
     */
    @PostMapping("allow")
    public Response allow(@RequestBody @Valid RequestMark form) {
        return requestService.process(form, true);
    }

    /**
     * 拒绝一个申请订单
     *
     * @param form Request标识实体
     * @return Response
     */
    @PostMapping("reject")
    public Response reject(@RequestBody @Valid RequestMark form) {
        return requestService.process(form, false);
    }

}
